//
//  AppDelegate.m
//  GCD
//
//  Created by YYY on 2017/12/28.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:[NSClassFromString(@"RootViewController") new]];
    self.window.rootViewController = navVC;
    [self.window makeKeyAndVisible];
    
    //冒泡排序
//    [self sort2];
//    //创建定时器
//    [self timer2];
    return YES;
}


/************************RunLoop***************************/
//创建RunLoop
- (void)createRunLoop
{
    //获取当前线程的runloop对象
    NSLog(@"NSRunLoop：当前线程的runloop对象: %@",[NSRunLoop currentRunLoop]);
    NSLog(@"CFRunLoopRef：当前线程的runloop对象: %@",CFRunLoopGetCurrent());
    
    //获取主线程的runloop对象
    NSLog(@"NSRunLoop：主线程的runloop对象: %@",[NSRunLoop mainRunLoop]);
    NSLog(@"CFRunLoopRef：主线程的runloop对象: %@",CFRunLoopGetMain());
    
    [[[NSThread alloc] initWithTarget:self selector:@selector(runing) object:nil] start];
}
- (void)runing
{
}

/************************创建定时器***************************/
//创建定时器
- (void)timer1
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0f target:self selector:@selector(repeat1) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)repeat1
{
    NSLog(@"当前模式： %@",[NSRunLoop currentRunLoop].currentMode);
    NSLog(@"当前线程： %@",[NSThread currentThread]);
}

//创建定时器
- (void)timer2
{
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(repeat1) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] run];
}

/************************冒泡排序、选择排序***************************/
//冒泡排序 降序排列
- (void)sort1
{
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@1, @4, @2, @3, @5, nil];
    for (int i=0; i<array.count; i++)
    {
        for (int j=0; j<array.count - 1; j++)
        {
            if (array[j] < array[j+1])
            {
                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
    }
    
    NSLog(@"冒泡排序：  %@",array);
}

//选择排序 降序排列
- (void)sort2
{
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@1, @4, @2, @3, @5, nil];

    for (int i=0; i<array.count; i++)
    {
        for (int j = i+1; j < array.count; j++)
        {
            if (array[i] < array[j])
            {
                [array exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    NSLog(@"选择排序：  %@",array);
}

@end
